package com.ytjj.qmyx.data.util;

import com.ytjj.common.exception.ApiException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

/**
 * 日历工具
 *
 * 
 *  *
 **/
public class CalendarUtil {

    public static void getTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(LocalDate.now().toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar ca = Calendar.getInstance();//日历对象
        ca.setTime(date);
        ca.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        //获取本月第一天
        String firstDay = sdf.format(ca.getTime());
        ca.setTime(date);
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        //获取本月最后一天
        String lastDay = sdf.format(ca.getTime());
        System.out.println("----------------------------");
        System.out.println(firstDay);
        System.out.println(lastDay);
    }

    public static String dateFormat(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    public static String dateFormatHour(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("HH");
        return df.format(date);
    }

    /**
     *  LocalDate转Date
     *  即获取当前天的开始时间
     *
     * @param localDate
     * @return
     */
    public static Date dateFormatParse(LocalDate localDate) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(localDate.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        throw new ApiException("时间解析异常");
    }


    /**
     * 获取某天的开始时间
     * @return
     */
    public static Date getStartTime(Date date) {
        Calendar todayStart = Calendar.getInstance();
        if(date != null) todayStart.setTime(date);
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取某天的结束时间
     * @return
     */
    public static Date getEndTime(Date date) {
        Calendar todayEnd = Calendar.getInstance();
        if(date != null) todayEnd.setTime(date);
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }



//    public static void main(String[] args) throws Exception {
////        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////        System.out.println(sdf.format(getStartTime(null)));
////        System.out.println(sdf.format(getEndTime(null)));
////        System.out.println(getStartTime(null).getTime());
////        System.out.println(getEndTime(null));
////
////        System.out.println(dateFormatParse(LocalDate
////                .now()).getTime());
//        LocalDate now = LocalDate.now();
//        LocalDate localDate = now.minusMonths(1);
//        System.out.println(localDate);
//        System.out.println(now);
//    }

    /**
     *  加减对应时间后的日期
     * @param date  需要加减时间的日期
     * @param amount    加减的时间(毫秒)
     * @return  加减对应时间后的日期
     */
    public static Date subtractTime(Date date, int amount) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String strTime = sdf.format(date.getTime() + amount);
            Date time = sdf.parse(strTime);
            return time;
        }catch (Exception e){
            e.printStackTrace();
        }
        throw new ApiException("时间解析异常");
    }

}
